<template>
  <LayoutContent>
    <div class="button">
      <div class="detail">{{ isEdit ? '编辑' : '新增' }}</div>
      <div>
        <el-button type="primary" @click="clickHandle('save')">保存</el-button>
        <el-button type="primary" @click="clickHandle('rest')">重置</el-button>
        <el-button @click="clickHandle('back')">返回</el-button>
      </div>
    </div>
    <div class="form">
      <el-form ref="formRef" :model="form" status-icon :rules="rules" label-position="left" label-width="auto">
        <el-form-item label="账户:">
          <el-input :value="form.account" disabled placeholder="" />
        </el-form-item>
        <el-form-item label="姓名:" prop="name">
          <el-input v-model.trim="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别:" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option label="男" :value="1" />
            <el-option label="女" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="年龄:" prop="age">
          <el-input v-model.trim="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="手机号码:" prop="phone">
          <el-input v-model.trim="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="邮箱:" prop="email">
          <el-input v-model.trim="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="创建时间:">
          <el-input v-model.trim="form.createTime" disabled placeholder="创建时间" />
        </el-form-item>
        <el-form-item label="修改时间:">
          <el-input v-model.trim="form.updateTime" disabled placeholder="修改时间" />
        </el-form-item>
      </el-form>
    </div>
  </LayoutContent>
</template>

<script setup>
import { onMounted, ref, reactive, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getDetailApi, addApi } from '@/api/teacher/index'
const route = useRoute()
const router = useRouter()

const formRef = ref(null)
const rules = reactive({
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'blur' }],
  age: [{ required: true, message: '请输入年龄', trigger: 'blur' }],
  phone: [
    { required: true, message: '手机号不能为空', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确格式的手机号', trigger: 'blur' },
  ],
  email: [
    { required: true, message: '邮箱不能为空', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, message: '请输入正确格式的邮箱', trigger: 'blur' },
  ],
})
const form = ref({})
const data = ref(null)
const isEdit = computed(() => route.query.id)
const getDetail = async () => {
  const res = await getDetailApi({ id: route.query.id })
  if (res.code == 200) {
    form.value = res.data
  } else {
    ElMessage.error(res.msg)
  }
}

onMounted(() => {
  if (isEdit.value) {
    getDetail()
  }
})
const submit = async () => {
  if (!formRef.value) return
  formRef.value.validate(async (valid) => {
    if (valid) {
      const res = await addApi(form.value)
      if (res.code == 200) {
        ElMessage.success(res.msg)
        router.back()
      } else {
        ElMessage.error(res.msg)
      }
    }
  })
}

const clickHandle = (type) => {
  switch (type) {
    case 'back':
      router.back()
      break
    case 'save':
      submit()
      break
    case 'rest':
      formRef.value.resetFields()
      break
  }
}
</script>

<style scoped lang="scss">
.content {
  .button {
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding-bottom: 10px;
    border-bottom: 2px solid #eee;
  }
  .form {
    margin-top: 10px;
  }
}
</style>
